//
//  HHXYLLocationPulseAnnotaionView.m
//  FunnyTicket
//
//  Created by 陶梦娇 on 17/2/10.
//  Copyright © 2017年 HH_Xiao. All rights reserved.
//

#import "HHXYLLocationPulseAnnotaionView.h"
#import "XYLHHPaoPaoView.h"

// 16进制转
#define UIColorFromRGBWithAlpha(rgbValue,alphaValue) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:alphaValue]

@interface HHXYLLocationPulseAnnotaionView ()
//!背景图片View
@property (nonatomic, strong) UIImageView *backImageView;
//!脉冲动画Layer
@property (nonatomic, strong) CALayer *pulseLayer;
//!脉冲底图Layer
@property (nonatomic, strong) CAShapeLayer *pulseBackLayer;
//!脉冲动画组
@property (nonatomic, strong) CAAnimationGroup *pulseAnimationGroup;

@property (nonatomic, strong) XYLHHPaoPaoView *customPaoPaoView;

@end

@implementation HHXYLLocationPulseAnnotaionView

#pragma mark - =====================懒加载====================
- (XYLHHPaoPaoView *)customPaoPaoView
{
    if (!_customPaoPaoView)
    {
        _customPaoPaoView = [[XYLHHPaoPaoView alloc] init];
    }
    return _customPaoPaoView;
}


- (void)setBackImage:(UIImage *)backImage
{
    _backImage = backImage;
    
    self.backImageView.image = backImage;
}

- (UIImageView *)backImageView
{
    if (!_backImageView)
    {
        _backImageView = [[UIImageView alloc] initWithFrame:self.bounds];
    }
    return _backImageView;
}
-(void) setBoundWidth:(float) width cornerRadius:(float) radius
{
    self.layer.cornerRadius = radius;
    self.layer.masksToBounds = YES;
    self.layer.borderWidth = width;
    self.layer.borderColor = [[UIColor lightGrayColor]CGColor];
}
- (UIImageView *)headerImageView
{
    if (!_headerImageView)
    {
        _headerImageView = [[UIImageView alloc] initWithFrame:CGRectMake(3, 2.5, 36, 36)];
        
        _headerImageView.layer.cornerRadius = 36/2.0;
        _headerImageView.layer.masksToBounds = YES;
        _headerImageView.layer.borderWidth = 0;
        _headerImageView.layer.borderColor = [[UIColor lightGrayColor]CGColor];
        
        //[_headerImageView setBoundWidth:0 cornerRadius:36/2.0];
    }
    return _headerImageView;
}
//!脉冲底图Layer
- (CAShapeLayer *)pulseBackLayer
{
    if (!_pulseBackLayer)
    {
        _pulseBackLayer = [CAShapeLayer layer];
        
        CGFloat width = self.bounds.size.width*self.pulseScaleFactor;
        
        _pulseBackLayer.bounds = CGRectMake(0, 0, width, width);
        
        _pulseBackLayer.position = CGPointMake(self.bounds.size.width/2, self.bounds.size.height);
        
        _pulseBackLayer.contentsScale = [UIScreen mainScreen].scale;
        // UIColorFromRGB(0xb1c9c1)
        _pulseBackLayer.fillColor = UIColorFromRGBWithAlpha(0xb1c9c1, 0.5).CGColor;
        
        _pulseBackLayer.lineWidth = 0.5;
        
        _pulseBackLayer.strokeColor = UIColorFromRGBWithAlpha(0x3981f0, 0.8).CGColor;
        
        UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:CGPointMake(width/2.0, width/2.0) radius:width/2.0 startAngle:0 endAngle:M_PI*2.0 clockwise:NO];
        
        _pulseBackLayer.path = [path CGPath];
    }
    return _pulseBackLayer;
}

//!脉冲Layer
- (CALayer *)pulseLayer
{
    if(!_pulseLayer)
    {
        _pulseLayer = [CALayer layer];
        
        CGFloat width = self.bounds.size.width*self.pulseScaleFactor;
        
        _pulseLayer.bounds = CGRectMake(0, 0, width, width);
        
        _pulseLayer.position = CGPointMake(self.bounds.size.width/2, self.bounds.size.height);
        
        _pulseLayer.contentsScale = [UIScreen mainScreen].scale;
        
        _pulseLayer.backgroundColor = self.pulseColor.CGColor;
        
        _pulseLayer.cornerRadius = width/2;
        
        _pulseLayer.opacity = 0;
        
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void)
                       {
                           if(self.delayBetweenPulseCycles != INFINITY)
                           {
                               CAAnimationGroup *animationGroup = self.pulseAnimationGroup;
                               
                               dispatch_async(dispatch_get_main_queue(), ^(void)
                                              {
                                                  [_pulseLayer addAnimation:animationGroup forKey:@"pulse"];
                                              });
                           }
                       });
    }
    return _pulseLayer;
}
//!脉冲动画组
- (CAAnimationGroup*)pulseAnimationGroup
{
    if(!_pulseAnimationGroup)
    {
        CAMediaTimingFunction *defaultCurve = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionDefault];
        
        _pulseAnimationGroup = [CAAnimationGroup animation];
        
        _pulseAnimationGroup.duration = self.outerPulseAnimationDuration + self.delayBetweenPulseCycles;
        
        _pulseAnimationGroup.repeatCount = INFINITY;
        
        _pulseAnimationGroup.removedOnCompletion = NO;
        
        _pulseAnimationGroup.timingFunction = defaultCurve;
        
        NSMutableArray *animations = [NSMutableArray new];
        
        CABasicAnimation *pulseAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale.xy"];
        
        pulseAnimation.fromValue = @0.0;
        
        pulseAnimation.toValue = @1.0;
        
        pulseAnimation.duration = self.outerPulseAnimationDuration;
        
        [animations addObject:pulseAnimation];
        
        CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"opacity"];
        
        animation.duration = self.outerPulseAnimationDuration;
        
        animation.values = @[@1.0, @0.45, @0];
        
        animation.keyTimes = @[@0, @0.2, @1];
        
        animation.removedOnCompletion = NO;
        
        [animations addObject:animation];
        
        _pulseAnimationGroup.animations = animations;
    }
    return _pulseAnimationGroup;
}
#pragma mark - =====================UIView方法====================
- (id)initWithAnnotation:(id<BMKAnnotation>)annotation reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithAnnotation:annotation reuseIdentifier:reuseIdentifier];
    
    if (self)
    {
        self.bounds = CGRectMake(0, 0, 42, 52);
        self.pulseScaleFactor = 5.3;
        
        self.pulseAnimationDuration = 2.0;
        
        self.outerPulseAnimationDuration = 3;
        
        self.delayBetweenPulseCycles = 0;
    }
    return self;
}

- (void)willMoveToSuperview:(UIView *)newSuperview
{
    if (newSuperview)
        [self reloadLayers];
}

#pragma mark - =====================加载Layer====================
- (void)reloadLayers
{
    //    [_pulseBackLayer removeFromSuperlayer];
    //
    //    _pulseBackLayer = nil;
    //
    //    [self.layer addSublayer:self.pulseBackLayer];
    
    [_pulseLayer removeFromSuperlayer];
    
    _pulseLayer = nil;
    
    _pulseAnimationGroup = nil;
    
    if (self.pulseColor)
    {
        [self.layer addSublayer:self.pulseLayer];
    }
    if (self.backImage)
        [self addSubview:self.backImageView];
    
    if (self.headerImageView)
        [self addSubview:self.headerImageView];
}

// 定位的泡泡
- (UIView *)setupItemAnnotationIsCanViewtitle:(NSString *)title subtitle:(NSString *)subtitle;
{
    
    UILabel *label = [self.customPaoPaoView paopaoWithTitle:title subtitle:subtitle];
    
    CGRect customFrame;
    
    if (label.frame.size.height < 25)
    {
        customFrame = CGRectMake(0, 0, label.frame.size.width+12+18, 33.5+25);
    }
    else
    {
        customFrame = CGRectMake(0, 0, label.frame.size.width+12+18, label.frame.size.height+25);
    }
    if (label.text.length > 0)
    {
        CGSize labelSize = [self labelTextAdjustment:label];
        label.frame = CGRectMake(6, 6, labelSize.width, labelSize.height);
    }
    else
    {
        label.frame = CGRectMake(6, 6, label.frame.size.width, label.frame.size.height);
    }
    
    [self.customPaoPaoView addConner:customFrame];
    
    self.customPaoPaoView.iconImageView.frame = CGRectMake(CGRectGetMaxX(label.frame), customFrame.size.height/2, 20, customFrame.size.height/2-15);
    
    [self.customPaoPaoView addNaviButton:self.customPaoPaoView.frame];
    
    return self.customPaoPaoView;
}
-(CGSize)labelTextAdjustment:(UILabel *)textLabel;
{
    //调整行间距
    NSMutableAttributedString * attributedString1 = [[NSMutableAttributedString alloc] initWithString:textLabel.text];
    NSMutableParagraphStyle * paragraphStyle1 = [[NSMutableParagraphStyle alloc] init];
    [paragraphStyle1 setLineSpacing:6];
    [attributedString1 addAttribute:NSParagraphStyleAttributeName value:paragraphStyle1 range:NSMakeRange(0, [textLabel.text length])];
    [textLabel setAttributedText:attributedString1];
    [textLabel sizeToFit];
    CGSize labelSize = [textLabel sizeThatFits:CGSizeMake(textLabel.frame.size.width, MAXFLOAT)];
    
    return labelSize;
}

@end
